package com.example.tms.tools;

import com.example.tms.bean.FileInfo;
import com.example.tms.bean.ReturnInfo;
import com.example.tms.mapper.RefMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 通用方法和变量
 */
@RestController
public class GlobalParam {

    private final RefMapper refMapper;

    @Autowired
    public GlobalParam(RefMapper refMapper){
        this.refMapper = refMapper;
    }

//    public static String filesposition = "com"; //files文件夹存放位置
    public static int num = 7;

    //获取Date时间
    public static Date getTime(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

    //获取星期
    public static String getWeek(String ddrq){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d = sdf.parse(ddrq);
            SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
            return dateFm.format(d);
        }catch (ParseException e){
            return "星期格式错误！";
        }
    }

    //生成订单号
    public static String getDdh(Long i){
        String str = i.toString();
        switch (str.length()){
            case 1:
                str = "000"+str;
                break;
            case 2:
                str = "00"+str;
                break;
            default:
                break;
        }
        return str;
    }

    //获取String日期
    public static String getSRq(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        return dateFm.format(date);
    }

    //获取String日期
    public static String getSRq(int num){
        Date date;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, num);
        date = cd.getTime();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        return dateFm.format(date);
    }

    //删除文件
    public static ReturnInfo deleteF(FileInfo fileInfo,String path){
        File file = new File(path, fileInfo.getFileurl());
        if(file.exists()){
            if(!file.delete()){
                return ReturnInfo.info1("删除失败！");
            }
            return ReturnInfo.info1("删除成功！");
        }else {
            return ReturnInfo.info1("文件不存在！");
        }
    }

    //判断是否能删除
    public Boolean deleteRef(String refid){
        Boolean b = false;
        if(refMapper.findOne(refid)==null||refMapper.findOne(refid).getRefnum()<=0){
            b = true;               //可以删除
            refMapper.delete(refid);//删除关联表数据
        }
        return b;
    }
    /**
     * 对字符串md5加密
     *
     * @param str
     * @return
     */
    public static String getMD5(String str) throws NoSuchAlgorithmException {

            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);

    }
    //map.put("", "0");输入格式错误
    //map.put("", "1");数据库中不存在
    //map.put("", "2");数据库中已存在
}
